var thisurl = top.window.location.href;
$(document).ready(function() {
	var begin = thisurl.indexOf("?");
	if (thisurl.length > begin) {
		var keyword= thisurl.substring(begin).replace("?word=", "");
		var tw = decodeURIComponent(keyword);
		while (tw.indexOf("+") > 0) {
			tw = tw.replace("+", " ");
		}
		$("#keyword")[0].value = tw;
		findItem(0, 0);
	}
});

function findItem(index, pagecount) {
	keyword = $.trim($("#keyword")[0].value);

	if (keyword.length > 0 && keyword.length < 200) {

		$.ajax({
					type : "post",
					dataType : "json",
					url : "Search",
					data : "word=" + keyword + "&index=" + index
							+ "&pagecount=" + pagecount,
					success : function(rsp) {
						searchCallback(rsp);
					}
				});

	}
}

function searchCallback(data) {
	var rsp = data.items;
	var pagecount = data.totalPage;
	var content = "";
	var len = rsp.length * 200;
	if (len < 800) {
		len = 800;
	}
	try {
		content += "<div class='item_brief'><table class='itemtb'>";
		for (i = 0; i < rsp.length; i++) {
			code = rsp[i].code;
			name = rsp[i].name;
			img = rsp[i].img;
			tags = rsp[i].tags;
			votenum = rsp[i].vote;
			commentnum = rsp[i].comment;

			content += "<tr><td rowspan='5' width='200px'><a target='_blank' href=jsp/item.jsp?itemid="
					+ code
					+ "><img width=120 src="
					+ img
					+ " /></a></td></tr>"
					+ "<tr>"
					+ "<td>\u59d3\u540d\uff1a</td><td><a target='_blank' href=jsp/item.jsp?itemid="
					+ code
					+ ">"
					+ name
					+ "</a></tr><tr><td>\u6807\u7b7e\uff1a</td><td>"
					+ tags
					+ "</td>"
					+ "</tr><tr><td>\u8bc4\u8bba\u6570\uff1a</td><td>"
					+ commentnum
					+ "</td>"
					+ "</tr><tr><td>\u6295\u7968\u6570\uff1a</td><td>"
					+ votenum + "</td></tr><tr><td  class='itemtb_td' colspan=3 style='width:800px;'></td></tr>";
		}
//		if (rsp.length == 0) {
			
//		}
	} catch (e) {
	}
	var page = "</table><div align='center'>";
	if (pagecount > 0) {
		page += "第";
		for (var i = 1; i <= (pagecount + 1); i++) {
			if (i % 20 == 0) {
				page += "<br/>";
			}
			page += "<a  href='javascript:findItem(" + (i - 1) + ","
					+ pagecount + ")'>" + i + " </a>";

		}
		page += "页";
	}

	content += page;
	
	content += "</div><div style='font-size:18px;font-family:SimHei; color:red;padding:10px;padding-top:40px;line-height: 30px;'  align='left'>没有找到您要找的人? <br\>" +
	"请确保您输入了要查找人的名字<br\>如果实在找不到 欢迎您作为第一个创建者来创建该人物<br\>" +
	"<a href='jsp/newitem.jsp'>现在就去创建！</a></div></div>";

	$("#middle").html("");
	$("#middle").append(content);
	$("#middle").css({
				"height" : len + "px"
			});
	//window.location.hash="pagetop";
}

function submit(){

	document.getElementById('searchform').submit();
}